package com.hdu.pyl.service.basic;

import com.hdu.pyl.mappper.NewsMapper;
import com.hdu.pyl.mappper.TagsMapper;
import com.hdu.pyl.model.ResultEnum;
import com.hdu.pyl.pojo.News;
import com.hdu.pyl.pojo.NewsTags;
import com.hdu.pyl.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService{
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private TagsMapper tagsMapper;
    @Override
    @Transactional
    public News findById(int newsId){
        News news=newsMapper.findById(newsId);
        if (null==news){
           return null;
        }
        news.setNewsTags(getTag(news.getId()));
        return news;
    }
    @Override
    @Transactional
    public News insertNew(News news){
        if (news==null||news.getTitle()==null||news.getAuthorId()==null){
            return null;
        }else {
            Date date=new Date();
            Timestamp timestamp=new Timestamp(date.getTime());
            news.setPostTime(timestamp);
            newsMapper.insertNew(news);
            news.setNewsTags(getTag(news.getId()));
            return news;
        }
    }

    @Override
    public List<News> findDesc(Integer limit) {
        return null;
    }

    @Override
    public List<News> findDesc(Integer limit, Date postTime) {
        return null;
    }

    @Override
    public List<News> newsCloudBoot(Integer limit) {
        return null;
    }

    @Override
    public List<News> newsCloudBoot(Integer limit, Date postTime) {
        return null;
    }

    @Override
    public List<News> notNewsCloudBoot(Integer limit) {
        return null;
    }

    @Override
    public List<News> notNewsCloudBoot(Integer limit, Date postTime) {
        return null;
    }


    private NewsTags getTag(Integer newsId){
        return tagsMapper.findByNewsId(newsId);
    }
}
